//
//  KXTabBarController.swift
//  开心微博
//
//  Created by 郑开心 on 15/12/5.
//  Copyright © 2015年 郑开心. All rights reserved.
//

import UIKit

class KXTabBarController: UITabBarController,TabBarDelegate{
    
    lazy var kxitems : [UITabBarItem] = {
        let items = [UITabBarItem]()
        return items
    }()

    lazy var home : HomeViewController = {
        let home = HomeViewController()
        return home
    }()

    lazy var message : MessageViewController = {
        let message = MessageViewController()
        return message
    }()
    lazy var discover : DiscoverViewController = {
        let discover = DiscoverViewController()
        return discover
    }()
    lazy var profile : ProfileViewControllerTableViewController = {
        let profile = ProfileViewControllerTableViewController()
        return profile
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpAllChildViewController()
        setUpTabBar()
        
        
//        NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "changTime", userInfo: nil, repeats: true)

        
        
    }
    
    func changTime(){
        UserTool.unreadWithUid({result in
            self.home.tabBarItem.badgeValue = String(result.status)
            
            self.message.tabBarItem.badgeValue = String(result.messageCount)
            self.profile.tabBarItem.badgeValue = String(result.follower)
            UIApplication.sharedApplication().applicationIconBadgeNumber =  result.messageCount
            
            
            },failure: {
                error in debugPrint(error)
        })
    }
    
    // MARK : 设置所有的子控制器
    func setUpAllChildViewController(){
        //主页
//        let home = HomeViewController()
        self.setUpOneChildViewController(home, titleName: "首页", itemImage: UIImage(named: "tabbar_home")!, itemSelectImage: UIImage.imageWithOriginalName("tabbar_home_selected")!)
        //self.addChildViewController(home)
        
        //消息
        //let message = MessageViewController()
        self.setUpOneChildViewController(message, titleName: "消息", itemImage: UIImage(named: "tabbar_message_center")!, itemSelectImage: UIImage.imageWithOriginalName("tabbar_message_center_selected")!)
        //message.view.backgroundColor = UIColor.blueColor()
       // self.addChildViewController(message)
        //发现
        //let discover = DiscoverViewController()
        self.setUpOneChildViewController(discover, titleName: "发现", itemImage: UIImage(named: "tabbar_discover")!, itemSelectImage: UIImage.imageWithOriginalName("tabbar_discover_selected")!)
        
        //discover.view.backgroundColor = UIColor.redColor()
        //self.addChildViewController(discover)
        //我
       // let profile = ProfileViewControllerTableViewController()
         self.setUpOneChildViewController(profile, titleName: "我", itemImage: UIImage(named: "tabbar_profile")!,itemSelectImage: UIImage.imageWithOriginalName("tabbar_profile_selected")!)
        //profile.view.backgroundColor = UIColor.greenColor()
        //self .addChildViewController(profile)

    }
    
    func setUpTabBar(){
        let tabBar = TabBar(frame: self.tabBar.bounds)
        tabBar.backgroundColor = UIColor.whiteColor()
        tabBar.myDelegate = self
        tabBar.kxitems = self.kxitems
        self.tabBar.addSubview(tabBar)
        //self.tabBar.removeFromSuperview()
    }
    
    func setUpOneChildViewController(viewController: UIViewController,titleName title: String,itemImage image: UIImage,itemSelectImage selectImage: UIImage){
        viewController.title = title
        //viewController.tabBarItem.badgeValue = "10"
        viewController.tabBarItem.image = image
        viewController.tabBarItem.selectedImage = selectImage
        
        kxitems.append(viewController.tabBarItem)
        
        let nav = KXNavigationControllerViewController(rootViewController: viewController)
        self.addChildViewController(nav)
        
    }
    
    func tabBar(tabeBar: TabBar, didClickButton index: Int) {
        self.selectedIndex = index
        if index == 0 {
            home.refresh()
        }
    }
    

}
